import Socket from './socket'
import MessageModel from '../../../../../models/web/messageModel'

const message = {
  state: {
    /**
     * socket
     */
    socketClient: new Socket(),
    /**
     * 消息数组
     */
    dataArr: [],
    /**
     * 未读消息数
     */
    unreadCount: 0,
    /**
     * 所有客户端
     */
    clients: [],
    /**
     * 访客姓名
     */
    visitorName: localStorage.getItem(process.env.VUE_APP_VISITOR_NAME_KEK),
    /**
     * 当前客户端ID
     */
    currentClientId: '',
  },
  mutations: {
    /**
     * shezhi dangqian yonghu ID
     */
    SET_CURRENT_CLIENT_ID (state, currentClientId) {
      state.currentClientId = currentClientId
    },
    /**
     * 添加消息
     */
    ADD_MESSAGE (state, messageData) {
      if (state.dataArr.length === 1000) {
        state.dataArr.shift()
      }
      const model = new MessageModel()
      model.setDataWithJson(messageData)
      state.dataArr.push(model)
    },
    /**
     * 设置未读消息数
     */
    SET_UNREAD_COUNT(state, isAdd) {
      if (isAdd) {
        state.unreadCount += 1
      } else {
        state.unreadCount = 0
      }
    },
    /**
     * 设置链接的用户数组
     */
    SET_SOCKET_CLIENTS (state, data) {
      state.clients = data.clients
    },
    /**
     * 设置访客名称
     */
    SET_VISITOR_NAME (state, name) {
      state.visitorName = name
      localStorage.setItem(process.env.VUE_APP_VISITOR_NAME_KEK, name)
    },
    /**
     * 发送消息
     */
    SEND_MESSAGE(state, message) {
      state.socketClient.socket.emit('message', {
        message: message,
        visitorName: state.visitorName
      })
    }
  },
  actions: {
  }
}

export default message
